/*
 * Copyright (C) 2012 Johan Boris Iantila jiantila@gmail.com && http://www.facebook.com/johanboris.iantila
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package mg.daloji.antandroyd.application;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;

import mg.daloji.antandroyd.R;
import mg.daloji.antandroyd.Radio;
import mg.daloji.antandroyd.Update;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;



/**
 *
 * @author Johan Boris Iantila
 *
 */


public class App extends Application {
	
	private static final String TAG = App.class.getSimpleName();
	static Configuration mConfiguration = null;
	private static AppLooper mInnerLooper = new AppLooper();
	private static Context mContext;

  
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = 
			        new StrictMode.ThreadPolicy.Builder().permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		mContext = getApplicationContext();
		mConfiguration = new Configuration(mContext);
		mInnerLooper.startService(false);
		setstat();
		
	}
	
	
	public static Context getContext(){
		return mContext;
	}

	public static LinkedList<Radio> getConf(){
		return mConfiguration.getAppConfiguration().getParam();
	}
	
	
	public static Update getupdate(){
		return mConfiguration.getAppConfiguration().getConfUpdate();
	}
	
	public static void add(int index,Radio radio){
		mConfiguration.setIndex(index, radio);
	}
	
	private void setstat(){
		URL url = null;
		String urlstat=getString(R.string.url_stat);
		String manufacturer = Build.MANUFACTURER;
		String model = Build.MODEL;
	    try {
	    	String registrationUrl =urlstat+String.format("?version=%s&manufacturer=%s&model=%s",Build.VERSION.SDK_INT,manufacturer,model);   	
	    	url = new URL(registrationUrl);
	        URLConnection connection = url.openConnection();
	        HttpURLConnection httpConnection = (HttpURLConnection) connection;
	        int responseCode = httpConnection.getResponseCode();
	        if (responseCode == HttpURLConnection.HTTP_OK) {
	            Log.d("Andtandroy", "Registration success");
	        } else {
	            Log.w("Andtandroy", "Registration failed for: " + registrationUrl);              
	        }
	    } catch (Exception ex) {
	        ex.printStackTrace();
	    }
	}
	
}
